﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using jnxc.Common;
using LTP.Accounts.Bus;
namespace jnxc.Web.product
{
    public partial class Add : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {

                CKFinder.FileBrowser fileBrowser = new CKFinder.FileBrowser();
                fileBrowser.BasePath = "../ckfinder/"; //设置CKFinder的基路径 
                fileBrowser.SetupCKEditor(ckcontent);

                if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
                {
                    int psid = (Convert.ToInt32(Request.Params["id"]));
                    ShowInfo(psid);
                }
                Page.DataBind();

            }

        }
        private void ShowInfo(int psid)
        {
            jnxc.BLL.product bll = new jnxc.BLL.product();
            jnxc.Model.product model = bll.GetModel(psid);
            this.hfcid.Value = model.cid.ToString();
            hfpid.Value = model.pid.ToString();
            ckcontent.Text = model.pcontent;
            ckpara.Text = model.ppara;
            txtpname.Text = model.pname;
            hfimg.Value=model.ppic;

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            int cid = 0,psid=0;
            if (hfcid.Value.ToString() != "")
            {

                cid = int.Parse(this.hfcid.Value);
                if (hfpid.Value.ToString() != "")
                {
                    psid = int.Parse(hfpid.Value.ToString());

                }
                string pname = this.txtpname.Text;
                string ppara = this.ckpara.Text;
                string pcontent = this.ckcontent.Text;
                int pstate = 1;
                string IndFile = "";
                bool iserror = false;
                if (fuIndFile.HasFile)
                {
                    String name = fuIndFile.FileName;
                    String exp = name.Substring(name.LastIndexOf('.')).ToLower();
                    String filename = DateTime.Now.ToString("yyyyMMddhhmmssfff");
                    if (exp == ".jpg" || exp == ".gif" || exp == ".png")
                    {
                        if (fuIndFile.PostedFile.ContentLength <= 50 * 1024 * 1024)
                        {
                            try
                            {
                                fuIndFile.PostedFile.SaveAs(Server.MapPath("~/propics/" + filename + exp));
                                IndFile = "/propics/" + filename + exp;
                            }
                            catch (Exception ex)
                            {
                                iserror = true;

                                ClientScript.RegisterClientScriptBlock(this.GetType(), "a", "alert('上传文件失败！');", true);
                            }
                        }
                        else
                        {
                            iserror = true;

                            ClientScript.RegisterClientScriptBlock(this.GetType(), "a", "alert('文件大于50m');", true);
                        }
                    }
                    else
                    {

                        ClientScript.RegisterClientScriptBlock(this.GetType(), "a", "alert('文件格式不正确');", true);

                        iserror = true;
                    }

                }
                else if (hfimg.Value != "")
                {
                    IndFile = hfimg.Value;

                }
                if (iserror == true)
                {
                    jnxc.Common.MessageBox.ShowAndRedirect(this, "保存失败！", "list.aspx");
                }
                else
                {
                    jnxc.Model.product model = new jnxc.Model.product();
                    model.cid = cid;
                    model.pname = pname;
                    model.ppara = ppara;
                    model.pcontent = pcontent;
                    model.pstate = pstate;
                    model.ppic = IndFile;
                    model.uid = getID();
                    model.pid = psid;
                    jnxc.BLL.product bll = new jnxc.BLL.product();
                    if (psid == 0)
                    {
                        if (bll.Add(model)>0)
                            jnxc.Common.MessageBox.ShowAndRedirect(this, "保存成功！", "list.aspx");
                    }
                    else
                    {
                        if(bll.Update(model))
                            jnxc.Common.MessageBox.ShowAndRedirect(this, "修改成功！", "list.aspx"); 
                    }
                    
                }
            }

        }


        public void btnCancle_Click(object sender, EventArgs e)
        {
            Response.Redirect("list.aspx");
        }
    }
}
